﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PerCiGal.BPC;

namespace PerCiGal.TestForm
{
	public partial class MainForm : Form
	{
	    internal Types.COLLECTION Collection;

		public MainForm()
		{
			InitializeComponent();
		}

		private void button_CreateCollection_Click(object sender, EventArgs e)
		{
			Types.COLLECTION_INFO collection_Info = new Types.COLLECTION_INFO();

			collection_Info.NAME = "Prova";

			collection_Info.VERSION = Common.ProductVersion.Version;

			collection_Info.DT_CREATION = DateTime.UtcNow;

			collection_Info.DT_LAST_MODIFIED = DateTime.UtcNow;

			collection_Info.FILE_PATH = textBox_FileCollection.Text;

            if (!string.IsNullOrEmpty(textBox_Password.Text))
                collection_Info.PWD = textBox_Password.Text;

            if (pictureBox1.Image != null)
                collection_Info.PICTURE = Common.FunctionImage.ByteArrayFromImage(pictureBox1.Image);

			BPC.BPCCollection collection = new BPC.BPCCollection();

			collection.Create(collection_Info);
		}

		private void button_InserMovie_Click(object sender, EventArgs e)
		{
			try
			{
				Types.COLLECTION_INFO collection_Info = new Types.COLLECTION_INFO();

				collection_Info.NAME = "Prova";

				collection_Info.VERSION = Common.ProductVersion.Version;

				collection_Info.DT_CREATION = DateTime.UtcNow;

				collection_Info.DT_LAST_MODIFIED = DateTime.UtcNow;

				collection_Info.FILE_PATH = @"c:\test\test.sdf";

				Types.MOVIE_DETAILS movie = new Types.MOVIE_DETAILS();

				movie.MAIN_TITLE = "prova";

			    movie.DT_CREATION = DateTime.Now;

			    movie.DT_LAST_MODIFIED = DateTime.UtcNow;

			    movie.GENRES = new List<int> {1};

			    var bpcMovie = new BPC.BPCMovie();

			    MessageBox.Show(bpcMovie.MovieDetailsInsert(collection_Info, movie).ToString());
			}
			catch (Exception exc)
			{
				MessageBox.Show(exc.ToString());
			}
		}

        private void button_OpenCollection_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = @"sdf Files |*.sdf";

            DialogResult result = openFileDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {
                textBox_FileCollection.Text = openFileDialog1.FileName;

                BPC.BPCCollection bpcCollection = new BPCCollection();

                Collection = bpcCollection.Open(textBox_FileCollection.Text, textBox_Password.Text);
            }

            LoadMovieCollection();
        }

        private void LoadMovieCollection()
        {
            foreach (var movie in Collection.MovieList.Items)
            {
                ListViewItem nItem = listView_Movie.Items.Add(movie.COD_MOVIE.ToString());
                nItem.SubItems.Add(movie.MAIN_TITLE);
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.Filter = @"Image Files |*.jpg; *.bmp; *.gif; *.png";

                DialogResult result = openFileDialog1.ShowDialog();

                if (result == DialogResult.OK)
                {
                    Image img = Image.FromFile(openFileDialog1.FileName);

                    pictureBox1.Image = img;
                }
            }
            catch (Exception error)
            {
                throw error;
            }
        }

        private void listView_Movie_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView_Movie.SelectedItems.Count > 0)
            {
                var bpcMovie = new BPCMovie();

                Types.MOVIE_DETAILS movieDetails = bpcMovie.MovieDetailsSelectByCodMovie(Collection.CollectionInfo ,int.Parse(listView_Movie.SelectedItems[0].Text));

                if (movieDetails != null)
                {
                    label_cod_movie.Text = movieDetails.COD_MOVIE.ToString();

                    label_main_title.Text = movieDetails.MAIN_TITLE;
                }
            }

        }

        private void button_WebMovieInfo_Click(object sender, EventArgs e)
        {
            WebMovieInfoForm webMovieInfoForm = new WebMovieInfoForm();

            webMovieInfoForm.Show();
        }
	}
}
